<?php
	
class User
{
	private $Id;
	private $Name;
	private $UserName;
	private $Password;
	private $Email;
	private $IsDeleted;
	private $Image;
	private $Description;
		
	public function getId()
	{
		if(!isset($this->Id))
			$this->setId(0);
		return $this->Id;
	}
	public function setId($id)
	{
		$this->Id = $id;
	}
	public function IsPersited()
	{
		return $this->getId() != 0;
	}
	
	public function getName()
	{
		if(!isset($this->Name))
			$this->setName('');
		return $this->Name;
	}
	public function setName($name)
	{
		$this->Name = $name;
	}
	
	public function getUserName()
	{
		if(!isset($this->UserName))
			$this->setUserName('');
		return $this->UserName;
	}
	public function setUserName($userName)
	{
		$this->UserName = $userName;
	}
	
	public function validatePassword($password)
	{
		$encoded = $this->encodePassword($password);
		return $this->Password == $encoded;
	}
	public function validateEncodedPassword($encoded)
	{
		return $this->Password == $encoded;
	}
	public function changePassword($password)
	{
		$this->Password = $this->encodePassword($password);
	}
	public function getPassword()
	{
		if(!isset($this->Password))
			$this->setPassword('');
		return $this->Password;
	}
	public function setPassword($password)
	{
		$this->Password = $password;
	}
	
	private function encodePassword($str)
	{
		return md5($str);
	}
	
	public function getEmail()
	{
		if(!isset($this->Email))
			$this->setEmail('');
		return $this->Email;
	}
	public function setEmail($email)
	{
		$this->Email = $email;
	}
	
	public function getIsDeleted()
	{
		if(!isset($this->IsDeleted))
			$this->setIsDeleted(false);
		return $this->IsDeleted;
	}
	public function setIsDeleted($isDeleted)
	{
		$this->IsDeleted = (bool)$isDeleted;
	}
        
        public function imageIsEmpty()
        {
            return !isset($this->Image) || $this->Image=='';
        }


        public function getImage($cache = true)
	{
		if(!isset($this->Image) || $this->Image=='')
			return baseUrl.'/uploads/default.png';
		else
                {
                    $noCache = isset($_SESSION['nocache']) ? $_SESSION['nocache'] : 0;
                    $firstStr = substr($this->Image, 0, 1);
                    $baseUrl = $firstStr == '/' ? baseUrl : baseUrl . '/';
                    $image = $baseUrl.$this->Image;
                    if($cache)
                        return $image.'?nocache='.$noCache;
                    else
                        return $image;
                }
	}
	public function setImage($image)
	{
		$this->Image = $image;
	}
	
	public function getDescription()
	{
		if(!isset($this->Description))
			$this->setDescription('');
		return $this->Description;
	}
	public function setDescription($description)
	{
		$this->Description = $description;
	}
}

?>